package viper.badRelays;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Hashtable;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


/**
 *
 * @author Kevin Sin
 * 
 * Revision number: 1
 * Date created: 1/17/10
 * Date of Last Back-up on SVN: none
 * 
 * <p>JPanel for when user first logs in.  Displays the peers online.
 */
public class RelaysPanel extends javax.swing.JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** Creates new form LoggedInJPanel */
	public RelaysPanel() {
		initComponents();
	}

	private javax.swing.JButton logoutButton; //Logout button
	private javax.swing.JButton clearButton; //Clear button
	
	private javax.swing.JLabel jLabel1; //delay
	private javax.swing.JLabel jLabel2; //drop rate 1
	private javax.swing.JLabel jLabel3; //drop rate 2
	private JLabel displayLabel1;
	private JLabel displayLabel2;
	private JLabel displayLabel3;
	private javax.swing.JSlider jSlider1; //delay
	private javax.swing.JSlider jSlider2; //drop rate 1
	private javax.swing.JSlider jSlider3; //drop rate 2
	
	private JLabel delayedLabel;
	private JLabel droppedLabel;

	/** This method is called from within the constructor to
	 * initialize the form.
	 */
	private void initComponents() {
		
		//box layout
    	setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    	setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

		logoutButton = new javax.swing.JButton();
		clearButton = new JButton("Clear");
		clearButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				BadRelaysStateManager.INSTANCE.clearStats();
				updateDisplay();
			}
		});
		

		logoutButton.setText("Log out");
		logoutButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				logoutButtonActionPerformed(evt);
			}
		});

		jLabel1 = new BigLabel();
		jLabel2 = new BigLabel();
		jLabel3 = new BigLabel();
		
		delayedLabel = new BigLabel("Delayed Packets: 0");
		droppedLabel = new BigLabel("Dropped Packets: 0");
		
		displayLabel1 = new BigLabel("0 ms");
		displayLabel2 = new BigLabel("0.0");
		displayLabel3 = new BigLabel("0.0");
		
		jSlider1 = new javax.swing.JSlider();
		jSlider2 = new javax.swing.JSlider();
		jSlider3 = new javax.swing.JSlider();

		Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
		labelTable.put( new Integer(0), new WhiteLabel("0 ms") );
		labelTable.put( new Integer(1000), new WhiteLabel("1000 ms") );
		labelTable.put( new Integer(2000), new WhiteLabel("2000 ms") );
		labelTable.put( new Integer(3000), new WhiteLabel("3000 ms") );
		
		jSlider1.setPaintLabels(true);
		jSlider1.setMinimum(0);
		jSlider1.setMaximum(3000);
		jSlider1.setLabelTable(labelTable);
		jSlider1.setMajorTickSpacing(500);
		jSlider1.setValue(0);
		jSlider1.addChangeListener(new ChangeListener() {	
			@Override
			public void stateChanged(ChangeEvent e) {
				int del = jSlider1.getValue();
				BadRelaysStateManager.INSTANCE.setDelay(del);
				displayLabel1.setText(del + " ms");
				displayLabel1.repaint();
			}
		});
		jSlider1.setPaintTicks(true);
		jSlider1.setBackground(Color.WHITE);
		jSlider1.setPreferredSize(new Dimension(500, 50));
		
		Hashtable<Integer, JLabel> labelTable2 = new Hashtable<Integer, JLabel>();
		labelTable2.put( new Integer(0), new WhiteLabel("0") );
		labelTable2.put( new Integer(25), new WhiteLabel(".25") );
		labelTable2.put( new Integer(50), new WhiteLabel(".5") );
		labelTable2.put( new Integer(75), new WhiteLabel(".75") );
		labelTable2.put( new Integer(100), new WhiteLabel("1") );
		jSlider2.setLabelTable(labelTable2);
		jSlider2.setBackground(Color.WHITE);
		jSlider2.setPaintLabels(true);
		jSlider2.setMinimum(0);
		jSlider2.setMaximum(100);
		jSlider2.setMajorTickSpacing(10);
		jSlider2.setValue(0);
		jSlider2.addChangeListener(new ChangeListener() {	
			@Override
			public void stateChanged(ChangeEvent e) {
				double dr1 = jSlider2.getValue()/100.0;
				BadRelaysStateManager.INSTANCE.setDrop_Rate_Hi(dr1);
				displayLabel2.setText("" + dr1);
				displayLabel2.repaint();
			}
		});
		jSlider2.setPaintTicks(true);
		jSlider2.setBackground(Color.WHITE);
		jSlider2.setPreferredSize(new Dimension(500, 50));
		

		Hashtable<Integer, JLabel> labelTable3 = new Hashtable<Integer, JLabel>();
		labelTable3.put( new Integer(0), new WhiteLabel("0") );
		labelTable3.put( new Integer(25), new WhiteLabel(".25") );
		labelTable3.put( new Integer(50), new WhiteLabel(".5") );
		labelTable3.put( new Integer(75), new WhiteLabel(".75") );
		labelTable3.put( new Integer(100), new WhiteLabel("1") );
		jSlider3.setLabelTable(labelTable3);
		jSlider3.setBackground(Color.WHITE);
		jSlider3.setPaintLabels(true);
		jSlider3.setMinimum(0);
		jSlider3.setMaximum(100);
		jSlider3.setMajorTickSpacing(10);
		jSlider3.setValue(0);
		jSlider3.addChangeListener(new ChangeListener() {	
			@Override
			public void stateChanged(ChangeEvent e) {
				double dr2 = jSlider3.getValue()/100.0;
				BadRelaysStateManager.INSTANCE.setTransition_Rate_toHi(dr2);
				displayLabel3.setText("" + dr2);
				displayLabel3.repaint();
			}
		});
		jSlider3.setPaintTicks(true);
		jSlider3.setBackground(Color.WHITE);
		jSlider3.setPreferredSize(new Dimension(500, 50));
		
		jLabel1.setText("Delay:");

		jLabel2.setText("Drop Rate:");

		jLabel3.setText("Transition Rate:");

		//logout button
        JPanel logoutPane = new JPanel();
        //logoutPane.setBackground(Color.WHITE);
        logoutPane.setLayout(new BoxLayout(logoutPane, BoxLayout.LINE_AXIS));
        logoutPane.add(Box.createHorizontalGlue());
        logoutPane.add(clearButton);
        logoutPane.add(Box.createHorizontalStrut(10));
        logoutPane.add(logoutButton, BorderLayout.LINE_END);
        
        //slider1 pane
        JPanel sliderPane1 = new JPanel();
        sliderPane1.setBackground(Color.WHITE);
        sliderPane1.setLayout(new BoxLayout(sliderPane1, BoxLayout.LINE_AXIS));
        sliderPane1.add(jLabel1, BorderLayout.PAGE_START);
        sliderPane1.add(Box.createHorizontalStrut(20));
        sliderPane1.add(displayLabel1);
        sliderPane1.add(Box.createHorizontalGlue());       
        //slider1_1 pane
        JPanel sliderPane1_1 = new JPanel();
        sliderPane1_1.setBackground(Color.WHITE);
        sliderPane1_1.setLayout(new BoxLayout(sliderPane1_1, BoxLayout.LINE_AXIS));
        sliderPane1_1.add(Box.createHorizontalGlue());
        sliderPane1_1.add(jSlider1, BorderLayout.LINE_END);
        sliderPane1_1.add(Box.createHorizontalStrut(20));
        
        //slider2 pane
        JPanel sliderPane2 = new JPanel();
        sliderPane2.setBackground(Color.WHITE);
        sliderPane2.setLayout(new BoxLayout(sliderPane2, BoxLayout.LINE_AXIS));
        sliderPane2.add(jLabel2, BorderLayout.PAGE_START);
        sliderPane2.add(Box.createHorizontalStrut(20));
        sliderPane2.add(displayLabel2);
        sliderPane2.add(Box.createHorizontalGlue());       
        //slider2_1 pane
        JPanel sliderPane2_1 = new JPanel();
        sliderPane2_1.setBackground(Color.WHITE);
        sliderPane2_1.setLayout(new BoxLayout(sliderPane2_1, BoxLayout.LINE_AXIS));
        sliderPane2_1.add(Box.createHorizontalGlue());
        sliderPane2_1.add(jSlider2, BorderLayout.LINE_END);
        sliderPane2_1.add(Box.createHorizontalStrut(20));
        
        //slider3 pane
        JPanel sliderPane3 = new JPanel();
        sliderPane3.setBackground(Color.WHITE);
        sliderPane3.setLayout(new BoxLayout(sliderPane3, BoxLayout.LINE_AXIS));
        sliderPane3.add(jLabel3, BorderLayout.PAGE_START);
        sliderPane3.add(Box.createHorizontalStrut(20));
        sliderPane3.add(displayLabel3);
        sliderPane3.add(Box.createHorizontalGlue());       
        //slider3_1 pane
        JPanel sliderPane3_1 = new JPanel();
        sliderPane3_1.setBackground(Color.WHITE);
        sliderPane3_1.setLayout(new BoxLayout(sliderPane3_1, BoxLayout.LINE_AXIS));
        sliderPane3_1.add(Box.createHorizontalGlue());
        sliderPane3_1.add(jSlider3, BorderLayout.LINE_END);
        sliderPane3_1.add(Box.createHorizontalStrut(20));

        
        JPanel bunch1 = new BunchPanel();
        bunch1.setLayout(new BoxLayout(bunch1, BoxLayout.PAGE_AXIS));
        bunch1.add(sliderPane1, BorderLayout.PAGE_START);
        bunch1.add(Box.createVerticalStrut(10));
        bunch1.add(sliderPane1_1, BorderLayout.PAGE_END);
        //bunch1.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        
        JPanel bunch2 = new BunchPanel();
        bunch2.setLayout(new BoxLayout(bunch2, BoxLayout.PAGE_AXIS));
        bunch2.add(sliderPane2, BorderLayout.PAGE_START);
        bunch2.add(Box.createVerticalStrut(10));
        bunch2.add(sliderPane2_1, BorderLayout.PAGE_END);
        //bunch2.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));

        JPanel bunch3 = new BunchPanel();
        bunch3.setLayout(new BoxLayout(bunch3, BoxLayout.PAGE_AXIS));
        bunch3.add(sliderPane3, BorderLayout.PAGE_START);
        bunch3.add(Box.createVerticalStrut(10));
        bunch3.add(sliderPane3_1, BorderLayout.PAGE_END);
        //bunch3.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        
        JPanel fillerPanel = new JPanel();        
        fillerPanel.setLayout(new BorderLayout());
        fillerPanel.setPreferredSize(new Dimension(700, 300));
        
        JPanel droppedAndDelayedPanel = new BunchPanel();
        //droppedAndDelayedPanel.setLayout(new BoxLayout(droppedAndDelayedPanel, BoxLayout.PAGE_AXIS));
        droppedAndDelayedPanel.setLayout(new BorderLayout());
        droppedAndDelayedPanel.add(delayedLabel, BorderLayout.LINE_START);
        droppedAndDelayedPanel.add(Box.createVerticalStrut(10));
        droppedAndDelayedPanel.add(droppedLabel, BorderLayout.AFTER_LAST_LINE);
        droppedAndDelayedPanel.setPreferredSize(new Dimension(700, 100));
        //droppedAndDelayedPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        
		add(logoutPane, BorderLayout.NORTH);
		
		add(Box.createVerticalStrut(25));
		add(bunch1, BorderLayout.AFTER_LAST_LINE);
		
		add(Box.createVerticalStrut(25));
		add(bunch2, BorderLayout.AFTER_LAST_LINE);
		
		add(Box.createVerticalStrut(25));
		add(bunch3, BorderLayout.AFTER_LAST_LINE);
		
		add(Box.createVerticalStrut(25));
		add(Box.createHorizontalStrut(0));
		add(droppedAndDelayedPanel, BorderLayout.AFTER_LAST_LINE);
		add(Box.createHorizontalGlue());
		
		add(fillerPanel, BorderLayout.AFTER_LAST_LINE);

		add(Box.createVerticalGlue());
	}

	/**
	 * @param evt
	 * Logout button pressed
	 */
	private void logoutButtonActionPerformed(java.awt.event.ActionEvent evt) {
		BadRelaysStateManager.INSTANCE.logout();
	}

	/**
	 * Updates the peers to display.
	 */
	public void update(){
	}
	
	/**
	 * For updating internal frame, which shows count of packets dropped and packets delayed.
	 */
	public void updateDisplay()
	{
		droppedLabel.setText("Dropped Packets: " + BadRelaysStateManager.INSTANCE.getDroppedPacketCount());
		delayedLabel.setText("Delayed Packets: " + BadRelaysStateManager.INSTANCE.getDelayedPacketCount());
		droppedLabel.repaint();
		delayedLabel.repaint();
	}

	public class WhiteLabel extends JLabel
	{
		private static final long serialVersionUID = 1L;

		public WhiteLabel()
		{
			super();
			setBackground(Color.WHITE);
			setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
		}

		public WhiteLabel(String string) {
			super(string);
			setBackground(Color.WHITE);
			setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
		}
	}
	
	public class BigLabel extends JLabel
	{
		private static final long serialVersionUID = 1L;

		public BigLabel()
		{
			super();
			setBackground(Color.WHITE);
			setFont(new Font(Font.SANS_SERIF, Font.BOLD, 22));
		}

		public BigLabel(String string) {
			super(string);
			setBackground(Color.WHITE);
			setFont(new Font(Font.SANS_SERIF, Font.BOLD, 22));
		}
	}
	
	public class BunchPanel extends JPanel
	{

		private static final long serialVersionUID = 1L;

		public BunchPanel()
		{
			super();
			setBackground(Color.WHITE);
			setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
			
		}
	}
	
}
